<?php

abstract class particularDAOImpl {

    public static function Select($search) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "select id, alertpay,cpf,
                name, id_city,cep,address,number,
                district, complement,contacts from customers";
            if (isset($search)) {
                if (is_int($search)) {
                    $query = $query . " where id=?";
                } else if (is_string($search)) {
                    $query = $query . " where name=?";
                }
            }
            DB::$_stmt = DB::$mysqli->prepare($query);
            if (isset($search)) {
                if (is_int($search)) {
                    DB::$_stmt->bind_param('i', $search);
                } else if (is_string($search)) {
                    DB::$_stmt->bind_param('s', $search);
                }
            }
            if (!DB::$_stmt->execute()) {
                DB::$mysqli->error;
            }
            DB::$_stmt->bind_result(
                    $data_id, $data_alertpay, $data_cpf, $data_name, $data_id_city, $data_cep, $data_address, $data_number, $data_district, $data_complement, $data_contacts
            );
            while (DB::$_stmt->fetch()) {
                $data[] = array(
                    'id' => $data_id,
                    'alertpay' => $data_alertpay,
                    'cpf' => $data_cpf,
                    'name' => $data_name,
                    'id_city' => $data_id_city,
                    'cep' => $data_cep,
                    'address' => $data_address,
                    'number' => $data_number,
                    'district' => $data_district,
                    'complement' => $data_complement,
                    'contacts' => $data_contacts
                );
            }
            return $data;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function SelectCustomersVehicles() {
        if (DB::$mysqli = DB::Connection()) {
            $query = "select cv.id,c.name as cliente, s.name , p.name as plano, p.price 
                from customers_vehicles cv 
                inner join status s on cv.id_status=s.id 
                inner join plans p on cv.id_plan=p.id 
                inner join customers c on cv.id_customer=c.id 
                inner join vehicles v on cv.id_vehicle=v.id 
                where (cv.id_status=1 or cv.id_status=4) and c.alertpay=1";
            DB::$_stmt = DB::$mysqli->prepare($query);
            if (!DB::$_stmt->execute()) {
                DB::$mysqli->error;
            }
            DB::$_stmt->bind_result(
                    $data_id, $data_name, $data_status, $data_planame, $data_price
            );
            while (DB::$_stmt->fetch()) {
                $data[] = array(
                    'id' => $data_id,
                    'name' => $data_name,
                    'status' => $data_status,
                    'planame' => $data_planame,
                    'price' => $data_price
                );
            }
            return $data;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Listar($limit = NULL, $page = NULL, $search = NULL) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "select id, alertpay,cpf,
                name, id_city,cep,address,number,
                district, complement,contacts from customers";
            if (isset($search)) {
                if (is_int($search)) {
                    $query = $query . " where id=?";
                } else if (is_string($search)) {
                    $query = $query . " where fancyname like ?";
                }
            }
            if (isset($limit)) {
                $query = $query . " limit " . $limit;
            }
            DB::$_stmt = DB::$mysqli->prepare($query);
            if (isset($search)) {
                if (is_numeric($search)) {
                    DB::$_stmt->bind_param('i', $search);
                } else {
                    DB::$_stmt->bind_param('s', $search);
                }
            }
            if (!DB::$_stmt->execute()) {
                DB::$mysqli->error;
            }
            DB::$_stmt->bind_result(
                    $data_id, $data_alertpay, $data_cpf, $data_name, $data_id_city, $data_cep, $data_address, $data_number, $data_district, $data_complement, $data_contacts
            );
            while (DB::$_stmt->fetch()) {
                $data[] = array(
                    'id' => $data_id,
                    'alertpay' => $data_alertpay,
                    'cpf' => $data_cpf,
                    'name' => $data_name,
                    'id_city' => $data_id_city,
                    'cep' => $data_cep,
                    'address' => $data_address,
                    'number' => $data_number,
                    'district' => $data_district,
                    'complement' => $data_complement,
                    'contacts' => $data_contacts
                );
            }
            return $data;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function UpdateStatus($id_status, $id) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "update customers_vehicles set id_status=?
                where id=?";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('ii', $id_status, $id);
            DB::$_stmt->execute();
            return DB::$_stmt;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Update($alertpay, $cpf, $name, $id_city, $cep, $address, $number, $district, $complement, $contacts, $id) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "update customers set alertpay=?,cpf=?,
                name=?, id_city=?,cep=?,address=?,number=?,
                district=?, complement=?,contacts=? where id = ?";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('idsiisisssi', $alertpay, $cpf, $name, $id_city, $cep, $address, $number, $district, $complement, $contacts, $id);
            DB::$_stmt->execute();
            return DB::$_stmt;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function UpdateAlertPay($alertpay, $id) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "update customers set alertpay=? where id=?";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('ii', $alertpay, $id);
            DB::$_stmt->execute();
            return DB::$_stmt;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Insert($alertpay, $cpf, $name, $id_city, $cep, $address, $number, $district, $complement, $contacts) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "insert into customers
                (id, id_status,id_plan,cnpj,fancyname,corporatename,
                id_city,cep,address,number,district,complement,contacts,
                filepicture,filebrand,website) 
                values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('idsiisisssi', $alertpay, $cpf, $name, $id_city, $cep, $address, $number, $district, $complement, $contacts);
            if (!DB::$_stmt->execute()) {
                echo DB::$mysqli->error;
            }
            return DB::$mysqli->insert_id;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Delete($id) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "delete from customers where id = ?";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('i', $id);
            DB::$_stmt->execute();
            return DB::$_stmt;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function CreateTransaction($id, $id_customers) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "insert into customers_transactions
                (id, id_customer_vehicle) 
                values (?,?)";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('si', $id, $id_customers);
            if (!DB::$_stmt->execute()) {
                echo DB::$mysqli->error;
            }
            return DB::$_stmt;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function SelectVehicles($search = NULL) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "select v.id,m.name as model,b.name as brand, vp.filename,v.featured
                        from customers c 
                        inner join customers_vehicles cv on cv.id_customer=c.id
                        inner join vehicles v on v.id=cv.id_vehicle
                        inner join vehicles_pictures vp on vp.id_vehicle=v.id
                        inner join models m on m.id=v.id_model
                        inner join brands b on b.id=m.id_brand ";
            if (isset($search)) {
                if (is_int($search)) {
                    $query = $query . " where c.id=? group by v.id";
                }
            }
            DB::$_stmt = DB::$mysqli->prepare($query);
            if (isset($search)) {
                if (is_int($search)) {
                    DB::$_stmt->bind_param('i', $search);
                }
            }
            if (!DB::$_stmt->execute()) {
                DB::$mysqli->error;
            }
            DB::$_stmt->bind_result(
                    $data_id, $data_model, $data_brand, $data_filename,$data_featured
            );
            while (DB::$_stmt->fetch()) {
                $data[] = array(
                    'id' => $data_id,
                    'model' => $data_model,
                    'brand' => $data_brand,
                    'filename' => $data_filename,
                    'featured'=>$data_featured
                );
            }
            return $data;
        } else {
            error_log("erro ao conectar!");
        }
    }

}

?>
